package com.ruitu.health_1.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.MD5Util;
import com.ruitu.arad.util.RegexUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.arad.view.EditTextCleanView;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.LoginModel;
import com.ruitu.health_1.mvp.presenter.LoginPresenter;
import com.ruitu.health_1.view.VerificationSeekBar;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.util.ReqUtil;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends BaseActivity<LoginPresenter.RegisterP, LoginModel.RegisterM> {

    private TextView tv_agreement;
    private TextView tv_register;
    private TextView tv_get_code;
    private CheckBox cb_password_show;
    private EditTextCleanView et_register_tel;
    private EditTextCleanView et_register_password;
    private EditTextCleanView et_register_password_code;
    private VerificationSeekBar seekBar;


    @Override
    protected int getLayoutRes() {
        return R.layout.activity_register;
    }

    private String from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        from = getIntent().getStringExtra("from");
        tv_agreement = findViewById(R.id.tv_agreement);
        tv_register = findViewById(R.id.tv_register);
        tv_get_code = findViewById(R.id.tv_get_code);
        cb_password_show = findViewById(R.id.cb_password_show);
        et_register_tel = findViewById(R.id.et_register_tel);
        et_register_password = findViewById(R.id.et_register_password);
        et_register_password_code = findViewById(R.id.et_register_password_code);
        seekBar = findViewById(R.id.seekBar);
        if (from != null) {
            setHeadTitle("忘记密码");
            tv_register.setText("修改");
        } else {
            setHeadTitle("注册账号");
        }
        setSpanText();
        setListener();
    }

    private String tel;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (tv_register == v) {
            Map<String, String> map = new HashMap<>();
            tel = et_register_tel.getText().toString();
            String password = et_register_password.getText().toString();
            String code = et_register_password_code.getText().toString();
            if (!RegexUtils.isMobileSimple(tel)) {
                ToastUtils.showShort("请输入正确的手机号");
                return;
            }
            if (!RegexUtils.isPassword(password)) {
                ToastUtils.showShort("请按照指示输入密码");
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtils.showShort("验证码不能为空");
                return;
            }
            //如果from为空是来自注册,如果不为空为忘记密码
            if (TextUtils.isEmpty(from)) {
                map.put("phone", tel);
                map.put("password", MD5Util.md5String(password));
                map.put("yzm", code);
                showProgress();
                p.register1(ReqUtil.getBody(map, true));
            }
        }

        if (tv_get_code == v) {
            Map<String, String> map = new HashMap<>();
            String tel = et_register_tel.getText().toString();
            if (!RegexUtils.isMobileSimple(tel)) {
                ToastUtils.showShort("请输入正确的手机号");
                return;
            }
            map.put("paramOne", tel);
            map.put("paramTwo", "1");
            showProgress();
            p.doyzm(ReqUtil.getBody(map, true));
        }
    }

    //获取验证码成功开始倒计时
    public void getCodeSuccess() {
        timer.start();
    }

    private void setListener() {
        setOnClickListener(tv_register, tv_get_code);
        //密码可见true可见false不可见
        cb_password_show.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    et_register_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    et_register_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Logg.i("Progress: " + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                if (seekBar.getProgress() < 100) {
                    seekBar.setProgress(0);
                }
            }
        });
    }

    private long getTime = 60000;
    //计时器监听
    private CountDownTimer timer = new CountDownTimer(getTime, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            T(millisUntilFinished / 1000 + "s后获取", false, getResources().getColor(R.color.text_gray));
        }

        @Override
        public void onFinish() {
            T("发送验证码", true, getResources().getColor(R.color.text_blue));
        }
    };

    private void T(String time, boolean i, int color) {
        tv_get_code.setText(time);
        tv_get_code.setEnabled(i);
        tv_get_code.setTextColor(color);
    }

    /**
     * 对于用户协议的分散式点击处理
     */
    private void setSpanText() {
        String te = "已阅读并同意以下协议:";
        String te1 = "xxxx服务协议";
        String te2 = "xxx政策";
        String te3 = "法律声明";
        SpannableStringBuilder ssb1 = new SpannableStringBuilder(te1);
        SpannableStringBuilder ssb2 = new SpannableStringBuilder(te2);
        SpannableStringBuilder ssb3 = new SpannableStringBuilder(te3);

        ssb1.setSpan(new SpanTextClickable(this, te1) {
            @Override
            public void onClick(@NonNull View widget) {
                ToastUtils.showShort("xxxx服务协议");
            }
        }, 0, te1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb2.setSpan(new SpanTextClickable(this, te2) {
            @Override
            public void onClick(@NonNull View widget) {
                ToastUtils.showShort("xxx政策");
            }
        }, 0, te2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ssb3.setSpan(new SpanTextClickable(this, te3) {
            @Override
            public void onClick(@NonNull View widget) {
                ToastUtils.showShort("法律声明");
            }
        }, 0, te3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv_agreement.setText(te);
        tv_agreement.append(ssb1);
        tv_agreement.append(",");
        tv_agreement.append(ssb2);
        tv_agreement.append(",");
        tv_agreement.append(ssb3);
        tv_agreement.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public void setRegisterSuc() {
        Arad.bus.post(new EventModel(19110700, tel));
        finish();
    }
}

abstract class SpanTextClickable extends ClickableSpan {
    private Context context;
    private String text;

    public SpanTextClickable(Context context, String text) {
        this.context = context;
        this.text = text;
    }

    public void updateDrawState(TextPaint textPaint) {
        textPaint.setColor(Color.RED);
        textPaint.setUnderlineText(true);
    }

}
